const express = require('express');
const router = express.Router();
const { body } = require('express-validator');
const membershipOrderController = require('../controllers/membershipOrderController');
const authMiddleware = require('../middleware/auth').authMiddleware;
const adminMiddleware = require('../middleware/auth').adminMiddleware;

// 创建会员订单（需要认证）
router.post('/', [
  authMiddleware,
  body('membership_level').notEmpty().withMessage('会员等级不能为空').isInt({ min: 1, max: 3 }).withMessage('会员等级必须是1-3之间的整数'),
  body('amount').notEmpty().withMessage('金额不能为空').isFloat({ min: 0 }).withMessage('金额必须是非负数')
], membershipOrderController.createMembershipOrder);

// 获取当前用户的会员订单列表（需要认证）
router.get('/me', authMiddleware, membershipOrderController.getUserMembershipOrders);

// 获取单个会员订单详情（需要认证且只能查看自己的订单）
router.get('/:id', authMiddleware, membershipOrderController.getMembershipOrderById);

// 管理员获取所有会员订单（需要管理员权限）
router.get('/', [authMiddleware, adminMiddleware], membershipOrderController.getAllMembershipOrders);

// 管理员更新订单状态（需要管理员权限）
router.put('/:id/status', [
  authMiddleware,
  adminMiddleware,
  body('status').notEmpty().withMessage('状态不能为空')
], membershipOrderController.updateOrderStatus);

module.exports = router;